"use strict";
cc._RF.push(module, '55e95d95kpIPLK3VAIyH9Pb', 'KSAPI');
// scripts/engines/services/miniGameContent/ks/KSAPI.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var miniApp_1 = require("../miniApp");
var gameEngine_1 = require("../../../gameEngine");
var ks = window["ks"];
var ksApi = /** @class */ (function (_super) {
    __extends(ksApi, _super);
    function ksApi() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.m_appId = 'ks675821421613799990';
        _this.m_secret = '820c32c3a9fff4c7b2db1720e30192132e5e2f07';
        _this.m_adUnitId = '2300003608_01';
        //录屏管理器
        _this.m_gameRecorderManager = null;
        //录屏文件保存完地址
        _this.m_gameRecordVideoUrl = null;
        //视频广告组件
        _this.m_videoAd = null;
        _this.canShare = true;
        _this.m_gameRecordTimeNow = 0;
        return _this;
    }
    ksApi.prototype.getSystemInfoSync = function () {
        throw new Error("Method not implemented.");
    };
    ksApi.prototype.WidgetfullScreen = function () {
        return;
    };
    ksApi.prototype.navigateToMiniGame = function (appId, successCallback, failCallback) {
        console.error("#501025 未实现跳转方法");
    };
    ksApi.prototype.commitRank = function (rankId, score) {
        return __awaiter(this, void 0, Promise, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, Promise.resolve(false)];
            });
        });
    };
    ksApi.prototype.showRank = function (rankId) {
        return __awaiter(this, void 0, Promise, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, Promise.resolve(false)];
            });
        });
    };
    ksApi.prototype.showModal = function (params) {
    };
    ksApi.prototype.LocalInBlockCityList = function () {
        return false;
    };
    ksApi.prototype.vibrate = function (type) {
    };
    ksApi.prototype.Init = function () {
        console.log("Init   start");
        // ks.init({ "appId": this.m_appId });
        // this.m_gameRecorderManager = ks.createMediaRecorder(); //-old
        this.m_gameRecorderManager = ks.getGameRecorder();
        if (this.m_gameRecorderManager === null) {
            console.log("当前版本 App 不支持录屏");
        }
        console.log("Init   over");
    };
    ksApi.prototype.Read = function () {
        // ks.readyGo();
        console.log("Read   over");
    };
    ksApi.prototype.LoadSubPackageLoadSubPack = function (packName, onComplete) {
        //let launchScene = "subRes"; //您的子包名称
        // var subpackageSupport = ks.isSupport(ks.Support.features.Subpackage);
        // if (!subpackageSupport) {
        //     //旧版本APP不支持，加载子包路径中的main.js即可，该路径与开发时设置的子包路径一致，如子包为sence2，路径为sence2/main.js
        //     window["require"]("subRes/main.js");
        //     onComplete && onComplete(false, "subRes");
        //     //执行你需要的逻辑，如加载场景
        //     return;
        // }
        var loadTask = ks.loadSubpackage({
            name: packName,
            success: function (res) {
                onComplete(false, res);
                console.log("分包加载成功");
                //执行你需要的逻辑，如加载场景
            }, fail: function (err) {
                onComplete(err, null);
                console.log(JSON.stringify(err));
            }
        });
        loadTask.onProgressUpdate(function (res) {
            console.log(res);
        });
    };
    ksApi.prototype.getUserInfo = function (params) {
        //ks.getUserInfo(params);
    };
    ksApi.prototype.showToast = function (params) {
        ///ks.showToast(params);
    };
    ksApi.prototype.showLoading = function (params) {
        //ks.showLoading(params);
    };
    ksApi.prototype.hideLoading = function () {
        //ks.hideLoading();
    };
    ksApi.prototype.preCreateUnits = function () {
        this.Init();
        this.Read();
        this.createRewardedVideoAd({ adUnitId: this.m_adUnitId });
        this.m_gameRecorderManager = ks.getGameRecorder();
    };
    ksApi.prototype.loadAndShowVideoAd = function (successCallback, errorCallback, completeCallback) {
        var _this = this;
        if (this.m_videoAd == null) {
            console.error('快手广告视频组件不存在');
            errorCallback(null);
            return;
        }
        this.m_videoAd.show()
            .then(function () {
            console.log("激励视频播放成功");
        });
        var onError = function (err) {
            errorCallback && errorCallback(err);
            console.log("激励视频播放失败: " + JSON.stringify(err));
        };
        this.m_videoAd.onError(onError);
        var onClose = function (res) {
            if (res && res.isEnded || res === undefined) {
                // 正常播放结束，可以下发游戏奖励
                successCallback && successCallback();
                console.log("激励视频奖励回调: " + JSON.stringify(res));
            }
            else {
                // 播放中途退出，不下发游戏奖励
                console.log("激励视频关闭回调: " + JSON.stringify(res));
            }
            _this.m_videoAd.offClose(onClose);
            _this.m_videoAd.offError(onError);
            completeCallback && completeCallback();
        };
        this.m_videoAd.onClose(onClose);
        //-old
        // //
        // this.m_videoAd.show({
        //     success: () => {
        //         console.log("激励视频播放成功");
        //     },
        //     fail: (result) => {
        //         errorCallback && errorCallback(result);
        //         console.log("激励视频播放失败: " + JSON.stringify(result));
        //     }
        // });
        // this.m_videoAd.onClose((result) => {
        //     completeCallback && completeCallback();
        //     //console.log("激励视频关闭回调: " + JSON.stringify(result));
        // });
        // this.m_videoAd.onReward((result) => {
        //     successCallback && successCallback();
        //     console.log("激励视频奖励回调: " + JSON.stringify(result));
        // });
    };
    ksApi.prototype.login = function (params) {
        ks.login(params);
    };
    ksApi.prototype.authorize = function (params) {
        params.success && params.success();
    };
    ksApi.prototype.gameRecordStart = function (params, onStart) {
        var _this = this;
        //if (!this.isHaveShareVideo()) return;
        this.m_gameRecordTimeNow = Date.now();
        this.m_gameRecordVideoUrl = null;
        this.m_gameRecorderManager.on("start", function (res) {
            console.log('ks 录屏开始', res);
            onStart && onStart(res);
        });
        this.m_gameRecorderManager.on("stop", function (res) {
            console.log('ks 录屏结束', res.videoID);
            _this.m_gameRecordVideoUrl = res.videoID;
        });
        this.m_gameRecorderManager.on("error", function (res) {
            console.log('ks 录屏错误 :', res.code, "-", res.msg);
        });
        this.m_gameRecorderManager.start();
    };
    ksApi.prototype.gameRecordStop = function () {
        //if (!this.isHaveShareVideo()) return;
        // if (Date.now() - this.m_gameRecordTimeNow < 3500) {
        //     return;
        // }
        this.canShare = true;
        this.m_gameRecorderManager.stop();
        return true;
    };
    ksApi.prototype.shareGameRecordVideo = function (successCb, failCb) {
        var _this = this;
        if (!this.isHaveShareVideo()) {
            gameEngine_1.default.getModules().uiControlModule.showMessageBox("不能重复分享");
            return;
        }
        /****注意：没有videoID时不可展示分享录屏按钮，审核会过此case****/
        /****测试方法：点击右上角"..."按钮打开设置页面，关闭录屏开关，录屏不会产生videoID****/
        if (this.m_gameRecordVideoUrl == null) {
            gameEngine_1.default.getModules().uiControlModule.showMessageBox("录屏失败，不能分享");
            return;
        }
        this.m_gameRecorderManager.publishVideo({
            video: this.m_gameRecordVideoUrl,
            callback: function (error) {
                if (error != null && error != undefined) {
                    console.log("分享录屏失败: " + JSON.stringify(error), error.code);
                    switch (error.code) {
                        case 0:
                        case -10018:
                            gameEngine_1.default.getModules().uiControlModule.showMessageBox("用户取消录屏");
                            break;
                        default:
                            failCb && failCb();
                            break;
                    }
                    return;
                }
                _this.canShare = false;
                console.log("分享录屏成功");
                successCb && successCb();
            }
        });
        // -old
        // this.m_gameRecorderManager.publishVideo({
        //     callback: (error) => {
        //         if (error != null && error != undefined) {
        //             console.log("分享录屏失败: " + JSON.stringify(error), error.code);
        //             switch (error.code) {
        //                 case 0:
        //                 case -10018:
        //                     O1HD.getModules().uiControlModule.showMessageBox("用户取消录屏");
        //                     break;
        //                 default:
        //                     failCb && failCb();
        //                     break;
        //             }
        //             return;
        //         }
        //         this.canShare = false;
        //         console.log("分享录屏成功");
        //         successCb && successCb();
        //     }
        // });
    };
    /**production    线上版
     development    测试版
     preview    预览版
     * */
    ksApi.prototype.isProduction = function () {
        return ks.env.VERSION == 'production';
    };
    ksApi.prototype.isHaveShareVideo = function () {
        return this.canShare;
    };
    //判断是否合法APP-ID
    ksApi.prototype.validAppId = function () {
        // ks.onShow((res: { query, refererInfo: { appId: string, extraData: {} } }) => {
        //     if (res.refererInfo.appId != this.m_appId) {
        //         cc.game.end();
        //         throw '非法盗用此程序将受到法律制裁';
        //     } else {
        //         console.log('ks APP-ID 授权成功');
        //     }
        // });
    };
    ksApi.prototype.reportAnalytics = function (EventKey, data) {
        return;
        console.log("ks 发送埋点", EventKey, JSON.stringify(data));
        ks.reportAnalytics(EventKey, data);
        // console.log('ks 发送埋点', analytics.getKey(), analytics.getObj());
        // ks.reportAnalytics(analytics.getKey(), analytics.getObj());
        // ks.reportAnalytics('lookVideo', {
        //     src: 'set_value',
        // });
    };
    ksApi.prototype.clearMemory = function () {
    };
    ksApi.prototype.shareAppMessage = function (args) {
    };
    ksApi.prototype.createRewardedVideoAd = function (param) {
        this.m_videoAd = ks.createRewardedVideoAd(param);
    };
    ksApi.prototype.PlayLastRecordSound = function (scale, successCb, errorCb) {
    };
    ksApi.prototype.StartRecordSound = function () {
    };
    ksApi.prototype.StopRecordSound = function (successCallback) {
    };
    ksApi.prototype.addShortcut = function () {
        return Promise.resolve(undefined);
    };
    ksApi.prototype.createMoreGameBtn = function () {
    };
    ksApi.prototype.drawUrlImage = function (url, overCb) {
    };
    ksApi.prototype.getShareVideoRank = function (sortType, successCb) {
    };
    ksApi.prototype.hideBanner = function () {
    };
    ksApi.prototype.operateGameLive = function () {
    };
    ksApi.prototype.reportAdClickNativeAd = function () {
    };
    ksApi.prototype.showAddFavoriteGuide = function (successCallback, failCallback, completeCallback) {
    };
    ksApi.prototype.showBannerAD = function (node, successCallback, errorCallback, onCloseCallBack, setBannerPosition) {
    };
    ksApi.prototype.showInterstitialAd = function (onAdClose, onFailed) {
    };
    ksApi.prototype.showNativeAd = function (successCallback, errorCallback, completeCallback) {
    };
    ksApi.prototype.showVideo = function (videoUrl) {
    };
    ksApi.prototype.subscribeMessage = function (successCallback, failCallback, completeCallback) {
    };
    ksApi.prototype.drawHead = function (headUrl) {
        return Promise.resolve(undefined);
    };
    ksApi.prototype.fullScreenVideo = function () {
        return Promise.resolve(undefined);
    };
    return ksApi;
}(miniApp_1.default));
exports.default = ksApi;

cc._RF.pop();